package copyengine.network.roomStructure.subMediator
{
	import copyengine.network.roomStructure.CENetworkCommandNotification;
	import copyengine.network.roomStructure.executeLayer.CENetworkRoomExecuteHandleNotification;
	import copyengine.utils.debug.CELog;

	import org.puremvc.as3.interfaces.INotification;
	import copyengine.network.roomStructure.data.CENetworkRoomMediatorWhiteBoard;

	public final class CENetworkRoomSubQuitMediator extends CENetworkRoomSubMediatorBasic
	{
		public function CENetworkRoomSubQuitMediator(_whiteBoard:CENetworkRoomMediatorWhiteBoard)
		{
			super(_whiteBoard);
		}

		override public function listNotificationInterests():Array
		{
			return [
				CENetworkCommandNotification.RECEIVE_COMMAND_QUIT_ROOM,
				CENetworkRoomExecuteHandleNotification.SEND_SOCKET_SERVER_CLOSED,
				CENetworkRoomExecuteHandleNotification.SEND_RECEIVE_INCOMING_DATA,
				];
		}

		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case CENetworkCommandNotification.RECEIVE_COMMAND_QUIT_ROOM:
					doQuitRoom();
					break;
				case CENetworkRoomExecuteHandleNotification.SEND_SOCKET_SERVER_CLOSED:
					onSocketServerDisconnectd();
					break;
				case CENetworkRoomExecuteHandleNotification.SEND_RECEIVE_INCOMING_DATA:
					onReceiveIncomingData(notification.getBody()["command"],
										  notification.getBody()["commandArg"],
										  notification.getBody()["forwardData"]);
					break;
			}
		}

		//====================
		//==Incoming Data
		//====================

		private function onReceiveIncomingData(_command:uint, _commandArg:Object, _forwardData:Object):void
		{
			switch (_command)
			{
				case CENetworkCommandNotification.S_RESERVE_COMMAND_QUIT_ROOM_RETURN:
					onQuitRoomCallBack(_commandArg);
					break;
			}
		}

		//=============
		//==Quit Room
		//=============

		/**
		 * 退出当前所在的房间(不需要传房间的ID和玩家的ID,在加入或者创建的时候会保留)
		 */
		private function doQuitRoom():void
		{
			var quitRoomCommandArg:Object =
				{
					uid:whiteBoard.playerUID,
					roomID:whiteBoard.roomID
				};
			sendSocketCommand(CENetworkCommandNotification.F_RESERVE_COMMAND_QUIT_ROOM, quitRoomCommandArg);
		}

		private function onQuitRoomCallBack(_arg:Object):void
		{
			var errorCode:uint = _arg["errorCode"];
			if (errorCode > 0)
			{
				CELog.err("CENetworkRoomMediator->onQuitRoomCallBack error code : " + errorCode);
			}
			tryToDisconnectSocketServer();
		}

		//=================
		//==Disconnect
		//=================

		private function tryToDisconnectSocketServer():void
		{
			sendNotification(CENetworkRoomExecuteHandleNotification.RECEIVE_END_CONNECT_SOCKET_SERVER);
		}

		private function onSocketServerDisconnectd():void
		{
			if (whiteBoard.isDuringCreateRoom)
			{
				sendNotification(CENetworkCommandNotification.SEND_CREATE_ROOM_FAILED);
			}
			else if (whiteBoard.isDuringJoinRoom)
			{
				sendNotification(CENetworkCommandNotification.SEND_JOIN_ROOM_FAILED);
			}
			else if (whiteBoard.isAlreadyInRoom)
			{
				sendNotification(CENetworkCommandNotification.SEND_QUIT_ROOM_SUCCEED);
			}
			else
			{
				CELog.err("CENetworkRoomSubQuitMediator->onSocketServerDisconnectd socket server disconnected in unexpect");
			}
			//清除好友数据
			whiteBoard.clean();
		}

	}
}
